package top.xunfong.file.config;

import top.xunfong.redis.listener.GlobalExpirationListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;

@Configuration
public class RedisConfig {

    @Autowired
    GlobalExpirationListener globalExpirationListener;

    @Bean
    public RedisMessageListenerContainer redisMessageListenerContainer(
            RedisConnectionFactory connectionFactory) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        // 监听过期事件
        container.addMessageListener(globalExpirationListener,
                new PatternTopic("__keyevent@0__:expired"));
        return container;
    }

//    @Bean
//    public MessageListenerAdapter breakpointFileExpirationListener() {
//        return new MessageListenerAdapter(new BreakpointFileExpirationListener());
//    }
}
